package com.tarzan.system.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tarzan.api.model.request.role.SysRoleIdReq;
import com.tarzan.api.model.request.role.UserBindingRoleReq;
import com.tarzan.api.model.request.user.RoleBindUserReq;
import com.tarzan.api.model.request.user.SysUserIdReq;
import com.tarzan.api.model.response.role.SysRoleVo;
import com.tarzan.api.model.response.user.UserRoleResp;
import com.tarzan.common.core.exception.BusinessException;
import com.tarzan.common.core.exception.ErrorCodesEnum;
import com.tarzan.system.dao.SysUserRoleMapper;
import com.tarzan.system.entity.SysUserRole;
import com.tarzan.system.service.ISysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liujinshan
 * @since 2020-05-19
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements ISysUserRoleService {

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 根据角色id查询角色id对应的用户
     *
     * @param roleIdReq
     * @return
     */
    @Override
    public IPage<UserRoleResp> getPageUserRoleInfoByRoleId(SysRoleIdReq roleIdReq) {

        Long id  = Optional.ofNullable(roleIdReq).map(SysRoleIdReq::getId).orElse(null);
        if (null == id) {
            throw new BusinessException(ErrorCodesEnum.REQUEST_PARAM_NOT_EMPTY_ERROR.code(),ErrorCodesEnum.REQUEST_PARAM_NOT_EMPTY_ERROR.msg()+"fild id is null");
        }
        Long pageSize  = Optional.ofNullable(roleIdReq).map(SysRoleIdReq::getPageSize).orElse(null);
        if (null == pageSize) {
            throw new BusinessException(ErrorCodesEnum.REQUEST_PARAM_NOT_EMPTY_ERROR.code(),ErrorCodesEnum.REQUEST_PARAM_NOT_EMPTY_ERROR.msg()+"fild pageSize is null");
        }
        Long currentPage  = Optional.ofNullable(roleIdReq).map(SysRoleIdReq::getCurrentPage).orElse(null);
        if (null == currentPage) {
            throw new BusinessException(ErrorCodesEnum.REQUEST_PARAM_NOT_EMPTY_ERROR.code(),ErrorCodesEnum.REQUEST_PARAM_NOT_EMPTY_ERROR.msg()+"fild currentPage is null");
        }
        Page<UserRoleResp> page = new Page<>(currentPage,pageSize);
        IPage<UserRoleResp> ps;
        try {
            ps = sysUserRoleMapper.getPageUserRoleInfoByRoleId(page,id);
        } catch (Exception e) {
            throw new BusinessException(ErrorCodesEnum.QUERY_ROLE_BINDING_USER_INFO_ERROR.code(),ErrorCodesEnum.QUERY_ROLE_BINDING_USER_INFO_ERROR.msg());
        }
        return ps;
    }

    /**
     * 插入角色绑定用户信息
     *
     * @param userBindingRoleReq
     */
    @Override
    public void insertUserBindingRole(UserBindingRoleReq userBindingRoleReq) {
       Integer roleId = Optional.ofNullable(userBindingRoleReq).map(UserBindingRoleReq::getRoleId).orElse(null);
        if (null == roleId) {
            throw new BusinessException(ErrorCodesEnum.REQUEST_PARAM_NOT_EMPTY_ERROR.code(),ErrorCodesEnum.REQUEST_PARAM_NOT_EMPTY_ERROR.msg()+"fild roleId is null");
        }
        try {
            for (int i = 0; i <userBindingRoleReq.getUserIds().size() ; i++) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(Integer.parseInt(userBindingRoleReq.getUserIds().get(i).toString()));
                sysUserRole.setRoleId(userBindingRoleReq.getRoleId());
                sysUserRoleMapper.insert(sysUserRole);
            }
        } catch (Exception e) {
            throw new BusinessException(ErrorCodesEnum.INSERT_ROLE_BINDING_USER_INFO_ERROR.code(),ErrorCodesEnum.INSERT_ROLE_BINDING_USER_INFO_ERROR.msg());
        }
    }

    /**
     * 根据用户ID查询角色信息
     *
     * @param sysUserIdReq
     * @return
     */
    @Override
    public List<SysRoleVo> findRoleInfoByUserId(SysUserIdReq sysUserIdReq) {
        List<SysRoleVo> list = new ArrayList<>();
        Long userId = Optional.ofNullable(sysUserIdReq).map(SysUserIdReq::getId).orElse(null);
        if (null == userId) {
            throw new BusinessException(ErrorCodesEnum.REQUEST_PARAM_NOT_EMPTY_ERROR.code(), ErrorCodesEnum.REQUEST_PARAM_NOT_EMPTY_ERROR.msg() + "fild roleId is null");
        }
        try {
            list = sysUserRoleMapper.findRoleInfoByUserId(userId);
        } catch (Exception e) {
            throw new BusinessException(ErrorCodesEnum.QUERY_ROLE_ERROR.code(),ErrorCodesEnum.QUERY_ROLE_ERROR.msg());
        }
        return list;
    }

    /**
     * 用户绑定角色信息插入
     *
     * @param req
     */
    @Override
    public void roleBindUser(RoleBindUserReq req) {
        Long userId = Optional.ofNullable(req).map(RoleBindUserReq::getUserId).orElse(null);
        if (null == userId) {
            throw new BusinessException(ErrorCodesEnum.REQUEST_PARAM_NOT_EMPTY_ERROR.code(),ErrorCodesEnum.REQUEST_PARAM_NOT_EMPTY_ERROR.msg()+"fild roleId is null");
        }
        try {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(req.getUserId());
            for (long roleIds: req.getRoleIds()) {
                sysUserRole.setRoleId(roleIds);
                sysUserRoleMapper.insert(sysUserRole);
            }
        } catch (Exception e) {
            throw new BusinessException(ErrorCodesEnum.INSERT_ROLE_BINDING_USER_INFO_ERROR.code(),ErrorCodesEnum.INSERT_ROLE_BINDING_USER_INFO_ERROR.msg());
        }
    }
}
